package com.crm.system.service.impl.cluePoolManager;


import com.crm.common.model.DhPageInfo;
import com.crm.common.model.Enum.StatusCodeEnum;
import com.crm.model.entity.cluePoolManager.ClueContact;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.qo.cluePoolManager.ClueContactQO;
import com.crm.model.vo.cluePoolManager.ClueContactVO;
import com.crm.service.cluePoolManager.ClueContactDbService;
import com.crm.system.service.cluePoolManager.ClueContactService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @ Author ：wll
 */
@Service
public class ClueContactServiceImpl implements ClueContactService {

    @Autowired
    private ClueContactDbService clueContactDbService;

    /**
     * @author wll
     * info 新增联系人记录
     */
    @Override
    public DhPageInfo<ClueContactVO> selectClueContactVOs(Integer pageIndex, Integer pageSize, String clueId) {
        if(pageIndex!=null&&pageSize!=null) {
            PageHelper.startPage(pageIndex, pageSize);
        }

        List<ClueContactVO> clueContactVOs = clueContactDbService.selectClueContactVOs(clueId);
        PageInfo<ClueContactVO> pageInfo = new PageInfo<ClueContactVO>(clueContactVOs);
        return new DhPageInfo<ClueContactVO>(pageInfo, clueContactVOs);
    }

    /**
     * @author wll
     * @date 2021/1/25 15:30
     * info 用户模块-我的线索池-线索清单-新增线索联系人
     * @param clueContactQO
     * @return
     */
    @Override
    public Boolean addClueContact(ClueContactQO clueContactQO) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        ClueContact clueContact = new ClueContact();
        BeanUtils.copyProperties(clueContactQO, clueContact);
        //处理数据
        clueContact.setClueContactId(UUID.randomUUID().toString().replaceAll("-", ""));
        clueContact.setDeleteStatus(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        clueContact.setCreatePeopleId(userAccount.getAccountId());
        clueContact.setCreateTm(new Date());
        clueContact.setUpdatePeopleId(userAccount.getAccountId());
        clueContact.setUpdateTm(new Date());
        Boolean result = clueContactDbService.insertClueContact(clueContact);
        return result;
    }


    /**
     * @author wll
     * @date 2021/1/26 16:32
     * info 用户模块-我的线索池-线索清单-跟进-线索联系人回显
     * @param status 0 查询全部联系人和手机号 1 模糊搜索联系人和手机号
     * @param clueId 线索id
     * @param param  status为1的时候 模糊搜索
     * @return
     */
    @Override
    public List<ClueContactVO> clueContactEcho(String status, String clueId, String param) {
        List<ClueContactVO> clueContactVOs = null;
        if("0".equals(status)){//查询线索的所有联系人信息
            param = "";
            clueContactVOs = clueContactDbService.selectClueContacts(clueId,param);
        }else{//模糊搜索线索的联系人信息
            clueContactVOs = clueContactDbService.selectClueContacts(clueId,param);
        }
        return clueContactVOs;
    }

    /**
     * 线索线上跟进判断联系人是否存在
     * @Author pym
     * @Date  2021/7/7 16:37
     * @param clueId 线索id
     * @param contactInformation 联系方式
     * @return {@link {com.crm.model.entity.cluePoolManager.ClueContact}}
     */
    @Override
    public ClueContact getClueContactByIdAndContactInfo(String clueId, String contactInformation) {
        return clueContactDbService.getClueContactByIdAndContactInfo(clueId,contactInformation);
    }

}
